//
//  AccountTicketVC.m
//  ImGuider
//
//  Created by llt on 2017/6/5.
//  Copyright © 2017年 imguider. All rights reserved.
//
#import "AccountTicketVC.h"
#import "AccountTicketCell.h"
#import "LinePreDetailVC.h"

@interface AccountTicketVC ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

//@property (weak, nonatomic) IBOutlet UIButton *noCouponBtn;
//
//@property (weak, nonatomic) IBOutlet UIView *getCouponView;
@property (weak, nonatomic) IBOutlet UITextField *couponTF;
@property (weak, nonatomic) IBOutlet UIButton *getCouponBtn;
@property (weak, nonatomic) IBOutlet UIView *headView;

@end

@implementation AccountTicketVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)setup {
    
    [self.getCouponBtn setTitle:MYLocalizedString(@"PUBLIC_CONFIRM", nil) forState:UIControlStateNormal];
    self.couponTF.placeholder = MYLocalizedString(@"VOUCHER_CODE_PLACEHOLDER", nil);
    
    [self configEmptyDataTableView:self.tableView emptyImageName:@"暂无优惠劵" describe:MYLocalizedString(@"PUBLIC_NODATA_VOUCHER", nil) refresh:nil block:nil];
    
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        
        [self loadNewData];
    }];
    
    if (self.pageType == 1) {
        self.title = MYLocalizedString(@"SELECTVOUCHER_TITLE", nil);
        
        self.dataHasLoad = YES;
        [self.tableView reloadData];
        return;
    }
    
    self.title = MYLocalizedString(@"ACCOUNT_VOUCHER", nil);
    
    
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [self loadNewData];
    
    [self.tableView setThemeStyle:ThemeStyleBlack];
}

- (void)loadNewData {
    
    @weakify(self);
    [NetAPIManager getMycouponsWithLineid:self.lineid Block:^(NSArray<Coupon *> *couponArray, NSError *error) {
        @strongify(self);
        
        self.dataHasLoad = YES;
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        [self.tableView.mj_header endRefreshing];
        
        self.dataSource = [NSMutableArray arrayWithArray:couponArray];
        [self.tableView reloadData];
    }];
}

- (IBAction)getCouponAction:(id)sender {
    
    if ([IGUtils isStringLenght0:self.couponTF.text]) {
        [IGUtils showToast:MYLocalizedString(@"VOUCHER_CODE_PLACEHOLDER", nil)];
        return;
    }
    [self.couponTF resignFirstResponder];
    [ActivityIndicatorView showHUDAddedTo:self.view];
    @weakify(self);
    [NetAPIManager redeemCouponCode:self.couponTF.text Block:^(NSArray <Coupon *>*couponArray, NSString *errmsg) {
        @strongify(self);
        
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        if (couponArray.count > 0) {
            
            Coupon *coupon = [couponArray firstObject];
            
            NSString *couponMsg = [NSString localizedStringWithFormat:MYLocalizedString(@"VOUCHER_SUCCESS", nil),[NSString stringWithFormat:@"%@%@",coupon.infos[@"big"],coupon.infos[@"middle"]],couponArray.count];
            
            NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];

            for (int i = 0; i < couponArray.count; i ++) {
                [indexes addIndex:i];
            }
            
            [self.dataSource insertObjects:couponArray atIndexes:indexes];
            [self.tableView reloadData];
            [IGUtils showToast:couponMsg];
            self.couponTF.text = @"";
        } else {
            
            [IGUtils showToast:errmsg];
        }
    }];
}

- (IBAction)noCouponAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    if ([_delegate respondsToSelector:@selector(selectCoupon:)]) {
        [_delegate selectCoupon:nil];
    }
}

#pragma mark -UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AccountTicketCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    Coupon *coupon = self.dataSource[indexPath.section];
    cell.pageType = self.pageType;
    cell.coupon = coupon;
    cell.delegate = self;
    
    return cell;
}

#pragma mark -UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.pageType == 1) {
        
        Coupon *coupon = self.dataSource[indexPath.section];
        
        if ([_delegate respondsToSelector:@selector(selectCoupon:)]) {
            [_delegate selectCoupon:coupon];
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - AccountTicketCellDelegate
- (void)useCouponCell:(AccountTicketCell *)cell coupon:(Coupon *)coupon {
    
    [ActivityIndicatorView showHUDAddedTo:self.view];
    @weakify(self);
    [NetAPIManager useCouponid:coupon.couponid Block:^(NSString *lineid, NSString *errmsg) {
        
        @strongify(self);
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        
        if (!errmsg) {
            if (coupon.type.integerValue == 9) { // 携程券
                
                
                [self.dataSource removeObject:coupon];
                [self.tableView reloadData];
                
                LinePreDetailVC *detailVC = [IGUtils loadViewController:@"LinePreDetailVC" storyboard:@"LineDetail"];
                detailVC.lineid = lineid;
                
                [self.navigationController pushViewController:detailVC animated:YES];
    
            }
        } else {
            
            [IGUtils showToast:errmsg];
        }
        
    }];
}
@end
